package com.xgkj.diyiketang.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.SchoolReportDeailActivity;
import com.xgkj.diyiketang.activity.data.SchoolReportAdapter;
import com.xgkj.diyiketang.data.AdvertiseInfo;
import com.xgkj.diyiketang.data.PengYouQuanInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.PengYouQuanListTask;
import com.xgkj.diyiketang.task.ShouCangReportListTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.Bannerview;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

public class ShouCangReportFragment extends BaseFragment implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener {
    private View cv = null;
    private Context mContext;
    private SchoolReportAdapter adapter;
    private PullToRefreshListView mPullToRefreshListView;
    private NoDataUtil mNoData;
    private List<PengYouQuanInfo> mYouQuanInfos;
    private int size = 10;
    private int currentPage = 1;
    private int anchorListTaskId = -1;
    private boolean isShow = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        cv = inflater.inflate(R.layout.fragment_shou_cang_report, container, false);
        mContext = getActivity();
        initview(cv);
//        getData();
//        getbanner();
        return cv;
    }

    private void initview(View v) {
        mPullToRefreshListView = (PullToRefreshListView) v.findViewById(R.id.schoolreport_listView);
        mPullToRefreshListView.init(getActivity(), true);
        adapter = new SchoolReportAdapter(mContext, 1);
        mPullToRefreshListView.setOnRefreshListener(this);
        mPullToRefreshListView.setOnLoadDataListener(this);
        RelativeLayout contentLayout = (RelativeLayout) v.findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext, contentLayout);
        mPullToRefreshListView.setAdapter(adapter);
        mPullToRefreshListView.doRefresh();
        mPullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), SchoolReportDeailActivity.class);
                intent.putExtra("rid", adapter.getItem(position - 1).getDairyId());
                try {
                    intent.putExtra("haveUp", URLDecoder.decode(adapter.getItem(position - 1).getHaveUp(), "utf-8"));
                    intent.putExtra("name", URLDecoder.decode(getEatShowApp().getmUserInfo().getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                startActivity(intent);
            }
        });
    }

    private void getData() {
        if (anchorListTaskId != -1) {
            return;
        }
        if (currentPage == 0) {
            mPullToRefreshListView.setNoNextPage();
            return;
        }
        ShouCangReportListTask task = new ShouCangReportListTask(size, currentPage);
        task.addListener(anchorListListener);
        anchorListTaskId = getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener anchorListListener = new ShowToastTaskEventListener(getActivity()) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == anchorListTaskId) {
                anchorListTaskId = -1;
                if (currentPage == 1) {
                    mPullToRefreshListView.onRefreshComplete();
                    mPullToRefreshListView.enableLoadMore();
                } else {
                    mPullToRefreshListView.onLoadDataComplete(true);
                }
                if (data != null) {
                    mYouQuanInfos = (List<PengYouQuanInfo>) data;
                    if (currentPage == 1) {
                        if (mPullToRefreshListView != null && mYouQuanInfos.size() > 0) {
                            adapter.updateData(mYouQuanInfos, true);
                        }

                    }
                    currentPage = result;
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == anchorListTaskId) {
                anchorListTaskId = -1;
                if (currentPage == 1) {
                    mPullToRefreshListView.onRefreshComplete();
                } else {
                    mPullToRefreshListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {

        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            //mListView.invalidate();
            if (adapter != null && adapter.getCount() == 0) {
                mPullToRefreshListView.doRefresh();
            }

        } else {
            isShow = false;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isShow) {
            currentPage = 1;
            getData();
        }

    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getData();
    }
}
